const { formatTime } = require('@/utils/util')

Page({
  onTap() {
    this.chooseImage()
  },

  chooseImage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      success: (res) => {
        const imagePath = res?.tempFiles[0]?.tempFilePath
        imagePath && this.onImageChosen(imagePath)
      }
    })
  },

  onImageChosen(imagePath) {
    // this.previewImage(imagePath)
    // this.rotate(imagePath)
    // this.addWatermark(imagePath)
    this.rotateAndAddWatermark(imagePath)
  },

  rotate(imagePath) {
    this.selectComponent("#image-comp")
      .rotate(imagePath, 90)
      .then(res => {
        this.previewImage(res)
      })
      .catch(err => {
        console.log(err)
      })
  },

  addWatermark(imagePath) {
    this.selectComponent("#image-comp")
      .addWatermark(imagePath, formatTime(new Date()))
      .then(res => {
        this.previewImage(res)
      })
      .catch(err => {
        console.log(err)
      })
  },

  rotateAndAddWatermark(imagePath) {
    this.selectComponent("#image-comp")
      .rotateAndAddWatermark(imagePath, 180, formatTime(new Date()))
      .then(res => {
        this.previewImage(res)
      })
      .catch(err => {
        console.log(err)
      })
  },

  previewImage(imagePath) {
    wx.previewImage({
      urls: [imagePath]
    })
  }
})